import Vue from "vue";
import Router from "vue-router"
import store from "../store/index"
import User from "../components/User.vue"
import Upload from "../components/upload.vue"
import Admin from "../components/admin.vue"
import Search from "../components/search.vue"
import Register from "../components/register.vue"
import Login from "../components/login"
import ChangePwd from "../components/changePwd.vue"
import AdminChange from "../components/adminChange.vue"
Vue.use(Router)

const router= new Router({
    linkActiveClass:"is-active",
    routes:[
        {path:"/",redirect:"/admin", name:"User", component:User},
        {path:"/upload", name:"Upload",meta:{requireAuth:true},  component:Upload},
        {path:"/admin",  redirect:"/admin/search", name:"Admin",
        component:Admin,children:[
            {path:"search",meta:{requireAuth:true}, component:Search},
            {path:"upload",meta:{requireAuth:true}, component:Upload},
            {path:"user",meta:{requireAuth:true},component:AdminChange},
        ]},
        {path:"/register",name:"Register",component:Register},
        {path:"/login",name:"Login",component:Login},
        {path:"/changePwd",name:"ChangePwd",component:ChangePwd},
        
    ],
    
})
//  let token=this.$store.state.token
//  console.log(token)
router.beforeEach((to,from,next)=>{
    if(to.meta.requireAuth){
        if(store.state.token){
            next()
        }else{
            next({
                path:"/login",
                query:{redirect:to.fullpath}
            })
        }
    }else{
        next()
    }
  })

export default router